﻿#include "controler.h"

Controler::Controler(QObject *parent) :
    QObject(parent),
    audioControler(new AudioControler(nullptr)),
    videoControler(new VideoControler(nullptr))
{


}





bool Controler::AudioControllerInit()
{
    QThread* audioThread = new QThread;
    audioControler->moveToThread(audioThread);

    connect(audioThread,&QThread::started,audioControler,&AudioControler::initialize);

    QEventLoop loop;
    connect(audioControler,&AudioControler::initialized_sig,&loop,&QEventLoop::quit);

    audioThread->start();
    qDebug() << "waiting for AudioControllerInit.....";
    loop.exec();// 进入事件循环，直到收到 initialized_sig 信号

    if(audioControler->getAudioControlerInit_state() == false)
    {
        qDebug() << "====AudioControlerInit fail====";
        return false;
    }
    else
    {
        qDebug() << "====AudioControlerInit success====";
        connect(this,&Controler::sendSpeechTeskToAudioController_sig,audioControler,&AudioControler::getSpeechTeskFromController_slot);
        return true;
    }
}


bool Controler::VideoControllerInit()
{
    videoThread = new QThread;
    videoControler->moveToThread(videoThread);

    connect(videoThread,&QThread::started,videoControler,&VideoControler::initialize);

    QEventLoop loop;
    connect(videoControler,&VideoControler::initialized_sig,&loop,&QEventLoop::quit);

    videoThread->start();
    qDebug() << "waiting for VideoControllerInit.....";
    loop.exec();

    if(videoControler->getVideoControlerInit_state() == false)
    {
        qDebug() << "====VideoControlerInit fail====";
        return false;
    }    
    else
    {
        qDebug() << "====VideoControlerInit success====";
        connect(videoControler,&VideoControler::sendSpeechTeskToController_sig,this,&Controler::sendSpeechTeskToAudioController_slot);
        return true;
    }
}

bool Controler::start_ObstacleDetection()
{
    videoControler->start_ObstacleDetection();
}


void Controler::sendSpeechTeskToAudioController_slot(QString str, int priority)
{
    emit sendSpeechTeskToAudioController_sig(str,priority);
}
